{head type="css"}
table.grid tr.total td {
	font-weight: bold;
	background-color: #eee;
	border-top: solid 2px black;
	border-bottom: solid 0px black;
}
{/head}
{assign var=report value=$reports.data}
{assign var=totals value=$reports.cat_totals.ds->toArray()}

{php}$this->_tpl_vars['lookup'] = array(){/php}
{foreach from=$totals item=row}
{php}$this->_tpl_vars['lookup'][$this->_tpl_vars['row']['provider_id']][$this->_tpl_vars['row']['cat_id']] = $this->_tpl_vars['row'];{/php}

{/foreach}

{$report.filter->render()}
{assign var=data value=$report.ds->toArray()}

{assign var=provider value=blah}
{assign var=providerId value=-999}
{assign var=categoryId value=-999}
{assign var=intable value=0}

{foreach from=$data item=row}
	{if $row.provider != $provider}
		{if $intable}
			{php}$this->_tpl_vars['totals'] = $this->_tpl_vars['lookup'][$this->_tpl_vars['providerId']][$this->_tpl_vars['categoryId']];{/php}
			<tr class="total">
				<td></td>
				<td>Total</td>
				<td>{$totals.count}</td>
				<td>{$totals.charges}</td>
				<td>{$totals.amount_paid}</td>
				<td>{$totals.average}</td>
			</tr>
			</tbody></table>
		{/if}

		{assign var=provider value=$row.provider}
		{assign var=providerId value=$row.provider_id}
		{assign var=intable value=0}
		<h2>{$row.provider}</h2><table cellpadding=0 cellspacing=1 class="grid">
	{assign var=categoryId value=""}
	{/if}

	{if $row.cat_id != $categoryId}
		{if $intable}
		{php}$this->_tpl_vars['totals'] = $this->_tpl_vars['lookup'][$this->_tpl_vars['providerId']][$this->_tpl_vars['categoryId']];{/php}
		<tr class="total">
			<td></td>
			<td>Total</td>
			<td>{$totals.count}</td>
			<td>{$totals.charges}</td>
			<td>{$totals.amount_paid}</td>
			<td>{$totals.average}</td>
		</tr>
		</tbody></table>
		{assign var=intable value=0}
		{/if}
		{assign var=categoryId value=$row.cat_id}
		<h3>{$row.category_name} ({$row.cat_id})</h3>
		<table cellpadding=0 cellspacing=1 class="grid">
			<thead>
				<th>Code</th>
				<th>Description</th>
				<th>#</th>
				<th>Charges</th>
				<th>Amount Paid</th>
				<th>Average</th>
			</thead>
			<tbody>
		{assign var=intable value=1}
		{assign var=index value=0}

	{/if}

	<tr{if $index%2 == 0} class="alt"{/if}>
		<td>{$row.code}</td>
		<td>{$row.description}</td>
		<td>{$row.count}</td>
		<td>{$row.charges}</td>
		<td>{$row.amount_paid}</td>
		<td>{$row.average}</td>
	</tr>
{/foreach}
		{php}$this->_tpl_vars['totals'] = $this->_tpl_vars['lookup'][$this->_tpl_vars['providerId']][$this->_tpl_vars['categoryId']];{/php}
		<tr class="total">
			<td></td>
			<td>Total</td>
			<td>{$totals.count}</td>
			<td>{$totals.charges}</td>
			<td>{$totals.amount_paid}</td>
			<td>{$totals.average}</td>
		</tr>
		</tbody></table>
